package com.zjcode.cdcer.service.impl;

import com.chinatechstar.component.commons.entity.MPage;
import com.chinatechstar.component.commons.result.PaginationBuilder;
import com.chinatechstar.component.commons.service.BaseService;
import com.chinatechstar.component.commons.supports.PageData;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zjcode.cdcer.mapper.CerArticleInfoMapper;
import com.zjcode.cdcer.model.CerArticleInfo;
import com.zjcode.cdcer.service.CerArticleInfoService;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName:CerArticleInfoServiceImpl
 * @功能：
 * @author：wlei
 * @日期：2022/9/23-23:31
 **/
@Service
public class CerArticleInfoServiceImpl implements CerArticleInfoService, BaseService {

    @Autowired
    private CerArticleInfoMapper cerArticleInfoMapper;

    @Override
    public int insert(CerArticleInfo record) {
        return cerArticleInfoMapper.insertSelective(record);
    }

    @Override
    public int update(CerArticleInfo record) {
        return cerArticleInfoMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int deleteById(String id) {
        return cerArticleInfoMapper.updateDeletedById(1, id);
    }

    @Override
    public Map<String, Object> listByPage(PageData pageData) {
        String keyword = (String) pageData.get("keyword");
        pageData.put("title", keyword);

        MPage mPage = getPageInfo(pageData);
        Page<Object> page = PageHelper.startPage(mPage.getCurrentPage(), mPage.getPageSize());
        List<LinkedHashMap<String, Object>> list = new ArrayList<>();
        list.addAll(cerArticleInfoMapper.listByPage(pageData));
        return PaginationBuilder.buildResult(list,
                                             page.getTotal(),
                                             mPage.getCurrentPage(),
                                             mPage.getPageSize());
    }

    @Override
    public List<CerArticleInfo> selectAllByGroupId(String group_id) {
        if (StringUtils.isNotEmpty(group_id)) {
            return cerArticleInfoMapper.selectAllByGroupId(group_id);
        }
        return new ArrayList<>();
    }
}
